package org.lzh.service;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.lzh.dao.UserDao;
import org.lzh.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.List;
import java.util.Map;

@Service
public class UserService {
    //注入dao
    @Autowired
    protected UserDao userDao;

    //根据id删除该用户
    public int deleteById(Integer id){
        return userDao.deleteById(id);
    }




    //根据id查找用户
    public User findById(String id){
        return userDao.findById(id);
    }

    //根据id更新用户
    public int updateById(User user){return  userDao.updateById(user);}

        //根据用户名查询是否有这个人，用于注册的用户名不能重名
    public User findByUsername(String username){
       return userDao.findByUsername(username);
    }

    //查询所有学生
    public List<User> findAllStudent1(){
        return userDao.findAllStudent();
    }

     //分页查询（通过数组）
    public List<User> findAllStudent(int currPage, int pageSize) {
        //查询全部数据
        List<User> students = userDao.findAllStudent();
        //总数据的长度
        int length=students.size();
        //从第几条数据开始
        int firstIndex = (currPage - 1) * pageSize;
        //到第几条数据结束
        int lastIndex = currPage * pageSize<length?currPage * pageSize:length;
        return students.subList(firstIndex, lastIndex); //直接在list中截取

    }

    //动态查询学生用户
    public List<User> findByDynamic1(User user){
        return userDao.findByDynamic(user);
    }

    //动态查询学生用户（分页）
    public List<User> findByDynamic(User user,int currPage, int pageSize) {
        //查询全部数据
        List<User> students = userDao.findByDynamic(user);
        //总数据的长度
        int length=students.size();
        //从第几条数据开始
        int firstIndex = (currPage - 1) * pageSize;
        //到第几条数据结束
        int lastIndex = currPage * pageSize<length?currPage * pageSize:length;
        return students.subList(firstIndex, lastIndex); //直接在list中截取

    }

    //查找成绩不为空的学生id
    public List<String> findAllIdScoreNotNull(){
        return userDao.findAllIdScoreNotNull();
    };

    //根据id动态更新
    public int updateByIdDynamic(User user){
        return userDao.updateByIdDynamic(user);
    };




    //导出excel表格
    //定义表头
    String[] excelHeader = {"学号","姓名", "分数", "班级"};

    public XSSFWorkbook export(List<User> list) {
        //新建一个XSSFWorkbook类 对象是wb
        //生成一个工作表
        //生成第一行
        //生成样式
        //循环把每列的属性设上去
        //循环设置每列应该输入的数据的值

        //这里需要说明一个问题：如果是 Offices 2007以前的Excel版本，new的对象是：**HSSFWorkbook** ，Offices 2007以后的Excel版本new的对象才是XSSFWorkbook
        XSSFWorkbook wb = new XSSFWorkbook();
        //生成一个工作表
        Sheet sheet = wb.createSheet("你好");
        //生成第一行
        Row row = sheet.createRow((int) 0);
        //生成单元格的样式style
        XSSFCellStyle style = wb.createCellStyle();
        //style.setAlignment(CellStyle.ALIGN_CENTER);

        for (int i = 0; i < excelHeader.length; i++) {
            //获取每一个单元格
            Cell cell = row.createCell(i);
            //给单元格赋值
            cell.setCellValue(excelHeader[i]);
            //设置单元格的样式
            cell.setCellStyle(style);
        }

        for (int i = 0; i < list.size(); i++) {
            //得到当前行数的下一行（row.getRowNum()：得到当前行数）
            row = sheet.createRow(row.getRowNum() + 1);
            User user = list.get(i);
            //赋值
            if(!user.getId().isEmpty()){
                row.createCell(0).setCellValue(user.getId());
            }
            if(!user.getUsername().isEmpty()){
                row.createCell(1).setCellValue(user.getUsername());
            }
            if(user.getScore()!=null){
                row.createCell(2).setCellValue(user.getScore());
            }
            if(user.getTeam()!=null){
                row.createCell(3).setCellValue(user.getTeam());
            }

        }
        return wb;
    }

}
